Arrays

 

Goals

Directions

  1. Log on to the NT workstation and grab the sample file
    1. Go to the example page at http://cs.wheatonma.edu/universal/javascript/arrays/example.html
    2. Open Arachnophilia, create a new html file called date3.html, and save it in the labs subfolder in the WWW folder of your NT dropbox. Now view the source for today’s demo, copy it, and paste it into your date3.html file. Make sure that you don’t have two copies of the <html>, <head>, and <body> tags.
    3. If you recall, the two lines
      var now = new Date();
      var day = now.getDay();
      will put a number 0 - 6 in the variable day, corresponding to the current day of the week, where 0=Sunday, 1=Monday, etc.
    4. In the script in this document, an array called dayNames is built to hold the names of the days. This array is then used in the document.write() statement to print out the correct day name.
  2. Add an array to hold the month names
    1. In the script in the head, create a new array monthNames that will hold the names of the months. Enter the names of the twelve months in positions 0 - 11 of the array, similar to the way the day names are entered in positions 0 - 6 of dayNames.
    2. You can get the current month (as a number between 0 and 11) by adding the line
      var month = now.getMonth();
      just before the end of the script.
    3. Modify the document.write( ) statement in the body of the document to print the name of the current month.
  3. Get the date and year
    1. In the same way, you can get the year and the day of the month by
        1. var year = now.getYear();
        2. var date = now.getDate();
    2. Add these lines just before the end of your script and modify your document.write() statement so that it prints out the complete phrase "It is Wednesday, April 22, 1998."

4) • Now add the time so it reads something like "It is Wednesday, April 22, 1998 at 13:00."

• Modify the time to read in standard time (as opposed to army time) with the appropriate AM and PM.

  1. If you have time. . .
    1. Modify your document.write() so that it handles the Year 2000 problem.
  2. When you have finished this
    1. Call the instructor or TA over to take a look at your work.
    2. Finally, LOGOUT OF THE MACHINE!!!